package com.project.xsf.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.project.JunM.entity.SCell;
import com.project.common.entity.PageVo;
import com.project.common.utils.Serial;
import com.project.xsf.entity.MDesignProcedure;
import com.project.xsf.entity.MDesignProcedureDetails;
import com.project.xsf.entity.MDesignProcedureModule;
import com.project.xsf.mapper.MDesignProcedureDetailsMapper;
import com.project.xsf.mapper.MDesignProcedureMapper;
import com.project.xsf.mapper.MDesignProcedureModuleMapper;
import com.project.xsf.service.MDesignProcedureService;
import com.project.xy.entity.DFile;
import com.project.xy.entity.DModuleDetails;
import com.project.xy.mapper.DFileMapper;
import com.project.xy.mapper.DModuleDetailsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class MDesignProcedureImpl implements MDesignProcedureService {
    @Autowired
    private MDesignProcedureMapper mDesignProcedureMapper;
    @Autowired
    private MDesignProcedureDetailsMapper mDesignProcedureDetailsMapper;
    @Autowired
    private DFileMapper dFileMapper;
    @Autowired
    private MDesignProcedureModuleMapper mDesignProcedureModuleMapper;
    @Autowired
    private DModuleDetailsMapper dModuleDetailsMapper;

    @Override
    public int insertSelective(MDesignProcedure record, List<MDesignProcedureDetails> mDesignProcedureDetailsList,DFile dFile) throws IOException {
        //往产品生产工序表表，产品生产工序明细表插数据，产品档案表的工序组成标志改变

        //该变工序组成标志：已设计
        dFile.setDESIGN_PROCEDURE_TAG("G001-1");
        dFileMapper.updateByPrimaryKeySelective(dFile);
        //等待审核:未审核
        record.setProcedureCheckTag("S001-0");
        //变更标志：未变更
        record.setChangeTag("B002-0");
        //工序物料设计标志：未设计
        record.setDesignModuleTag("G002-0");
        //工序物料变更标志：未变更
        record.setDesignModuleChangeTag("G003-0");
        //设置工序设计单编号
        Serial serial = new Serial();
        serial.addUser("designId","201");
        String designId = serial.getNumber("designId", "201", null, null, null);
        record.setDesignId(designId);

        record.setFirstKindId(dFile.getFIRST_KIND_ID());
        record.setFirstKindName(dFile.getFIRST_KIND_NAME());
        record.setSecondKindId(dFile.getSECOND_KIND_ID());
        record.setSecondKindName(dFile.getSECOND_KIND_NAME());
        record.setThirdKindId(dFile.getTHIRD_KIND_ID());
        record.setThirdKindName(dFile.getTHIRD_KIND_NAME());
        int rows = mDesignProcedureMapper.insertSelective(record);

        Integer parentId = record.getId();
        for(int i=0;i<mDesignProcedureDetailsList.size();i++){
            MDesignProcedureDetails mDesignProcedureDetails = mDesignProcedureDetailsList.get(i);
            mDesignProcedureDetails.setParentId(parentId);
            mDesignProcedureDetails.setDesignModuleTag("D002-0");
            mDesignProcedureDetails.setDesignModuleChangeTag("D003-0");
            mDesignProcedureDetailsMapper.insertSelective(mDesignProcedureDetails);
        }
        return rows;
    }

    //产品定义人员查询到所有待审核的工序组成设计单
    @Override
    public PageVo<MDesignProcedure> queryMDesignProcedure(Map<String,String> map) {
        Integer pageNum = Integer.valueOf(map.get("pageNum"));
        Integer pageSize= Integer.valueOf(map.get("pageSize"));

        PageHelper.startPage(pageNum,pageSize);
        List<MDesignProcedure> mDesignProcedures = mDesignProcedureMapper.queryMDesignProcedure();
        PageInfo pageInfo = new PageInfo(mDesignProcedures);

        PageVo<MDesignProcedure> pageVo = new PageVo<>();
        pageVo.setTotal(pageInfo.getTotal());
        pageVo.setRows(pageInfo.getList());
        return pageVo;
    }

    @Override
    public int updateByPrimaryKeySelective(MDesignProcedure record,List<MDesignProcedureDetails> mDesignProcedureDetailsList) {
        for(int i=0; i<mDesignProcedureDetailsList.size();i++){
            MDesignProcedureDetails mDesignProcedureDetails = mDesignProcedureDetailsList.get(i);
            mDesignProcedureDetailsMapper.updateByPrimaryKeySelective(mDesignProcedureDetails);
        }
//        record.setCheckTag("");
        return  mDesignProcedureMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKeySelective(MDesignProcedure record) {
        return mDesignProcedureMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public PageVo<MDesignProcedure> queryAllMDesignProcedure(Map<String,String> map) {
        Integer pageNum = Integer.valueOf(map.get("pageNum"));
        Integer pageSize= Integer.valueOf(map.get("pageSize"));

        PageHelper.startPage(pageNum,pageSize);
        List<MDesignProcedure> mDesignProcedures = mDesignProcedureMapper.queryAllMDesignProcedure();
        PageInfo pageInfo = new PageInfo(mDesignProcedures);

        PageVo<MDesignProcedure> pageVo = new PageVo<>();
        pageVo.setTotal(pageInfo.getTotal());
        pageVo.setRows(pageInfo.getList());
        return pageVo;
    }

    @Override
    public PageVo<MDesignProcedure> queryGreenMDesignProcedure(Map<String, String> map) {
        Integer pageNum = Integer.valueOf(map.get("pageNum"));
        Integer pageSize= Integer.valueOf(map.get("pageSize"));

        PageHelper.startPage(pageNum,pageSize);
        List<MDesignProcedure> mDesignProcedures = mDesignProcedureMapper.queryGreenMDesignProcedure();
        PageInfo<MDesignProcedure> mDesignProcedurePageInfo = new PageInfo<>(mDesignProcedures);

        PageVo<MDesignProcedure> pageVo = new PageVo<>();
        pageVo.setRows(mDesignProcedurePageInfo.getList());
        pageVo.setTotal(mDesignProcedurePageInfo.getTotal());

        return pageVo;

    }

    @Override
    public int changeProcedure(MDesignProcedure record, List<MDesignProcedureDetails> mDesignProcedureDetailsList) {
        List<DModuleDetails> dModuleDetails = dModuleDetailsMapper.queryDModuleDetailsByProductId(record.getProductId());
        for(int i=0; i<dModuleDetails.size();i++){
            DModuleDetails dModuleDetails1 = dModuleDetails.get(i);
            dModuleDetails1.setDesidualAmount(dModuleDetails1.getAmount());
            dModuleDetailsMapper.updateByPrimaryKeySelective(dModuleDetails1);
        }

        for(int i=0; i<mDesignProcedureDetailsList.size();i++){
            MDesignProcedureDetails mDesignProcedureDetails = mDesignProcedureDetailsList.get(i);
            //当前工序物料标志D002-0: 未设计
            mDesignProcedureDetails.setDesignModuleTag("D002-0");
            mDesignProcedureDetailsMapper.updateByPrimaryKeySelective(mDesignProcedureDetails);

        }
        record.setProcedureCheckTag("S001-0");
        record.setModuleCheckTag("");
        //删除物料设计明细表
        mDesignProcedureModuleMapper.deleteByPId(record.getId());
        //工序物料设计标志：未设计
        record.setDesignModuleTag("G002-0");
        return  mDesignProcedureMapper.updateByPrimaryKeySelective(record);
    }
    @Override
    public PageVo<MDesignProcedure> queryMDesignProcedureModule(Map<String,String> map) {
        Integer pageNum = Integer.valueOf(map.get("pageNum"));
        Integer pageSize= Integer.valueOf(map.get("pageSize"));

        PageHelper.startPage(pageNum,pageSize);
        List<MDesignProcedure> mDesignProcedures = mDesignProcedureMapper.queryMDesignProcedureModule();
        PageInfo<MDesignProcedure> pageInfo = new PageInfo<>(mDesignProcedures);

        PageVo<MDesignProcedure> pageVo = new PageVo<>();
        pageVo.setTotal(pageInfo.getTotal());
        pageVo.setRows(pageInfo.getList());
        return pageVo;
    }

    @Override
    public MDesignProcedure selectByPrimaryKey(Integer id) {
        return mDesignProcedureMapper.selectByPrimaryKey(id);
    }

    @Override
    public MDesignProcedure queryByProductId(String productId) {
        return mDesignProcedureMapper.queryByProductId(productId);
    }

    @Override
    public PageVo<MDesignProcedure> queryMDesignProcedureModuleNoCheck(Map<String, String> map) {
        Integer pageNum = Integer.valueOf(map.get("pageNum"));
        Integer pageSize= Integer.valueOf(map.get("pageSize"));

        PageHelper.startPage(pageNum,pageSize);
        List<MDesignProcedure> mDesignProcedures = mDesignProcedureMapper.queryMDesignProcedureModuleNoCheck();
        PageInfo<MDesignProcedure> mDesignProcedurePageInfo = new PageInfo<MDesignProcedure>(mDesignProcedures);

        PageVo<MDesignProcedure> pageVo = new PageVo<MDesignProcedure>();
        pageVo.setRows(mDesignProcedurePageInfo.getList());
        pageVo.setTotal(mDesignProcedurePageInfo.getTotal());

        return pageVo;
    }

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return mDesignProcedureMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageVo<MDesignProcedure> queryMDesignProcedureHasDesign(Map<String, String> map) {
        Integer pageNum = Integer.valueOf(map.get("pageNum"));
        Integer pageSize= Integer.valueOf(map.get("pageSize"));

        PageHelper.startPage(pageNum,pageSize);
        List<MDesignProcedure> mDesignProcedures = mDesignProcedureMapper.queryMDesignProcedureHasDesign();
        PageInfo<MDesignProcedure> pageInfo = new PageInfo<>(mDesignProcedures);

        PageVo<MDesignProcedure> pageVo = new PageVo<MDesignProcedure>();
        pageVo.setRows(pageInfo.getList());
        pageVo.setTotal(pageInfo.getTotal());

        return pageVo;
    }
}
